package Datebase;

import javax.swing.table.DefaultTableCellRenderer;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import javax.swing.text.StyledDocument;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class smessage extends JFrame {

    JFrame frame3;
    public static void main(String[] args) {
        System.setProperty("sun.java2d.noddraw", "true");
        new smessage("sno");
    }
    public smessage(String sno)
    {
        frame3 = new JFrame("个人信息系统");
        //图标
        URL resource = smessage.class.getClassLoader().getResource("kun.png");
        Image image = new ImageIcon(resource).getImage();
        frame3.setIconImage(image);
        //窗口尺寸
        frame3.setSize(800, 600);
        frame3.setLocationRelativeTo(null);
        frame3.setVisible(true);

        frame3.setLayout(null);


//创建表格
                try {
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
                    String user = "HR";
                    String password = "root";
                    Connection conn = DriverManager.getConnection(dbURL, user, password);
                    String sql = "select * from STUDENT where SNO = '" + sno+ "'";
                    PreparedStatement pstm = conn.prepareStatement(sql);
                    ResultSet rs = pstm.executeQuery();
                    int count = 0;
                    while (rs.next()) {
                        count++;
                    }
                    rs = pstm.executeQuery();

                    Object[][] data = new Object[count][7];
                    String[] title = {"学号", "姓名", "性别", "系", "专业", "班级"};
                    count = 0;
                    while (rs.next()) {
                        data[count][0] = (rs.getString("SNO"));

                        data[count][1] = rs.getString("SNAME");
                        data[count][2] = rs.getString("SSEX");
                        data[count][3] = rs.getString("DEPARTMENT");
                        data[count][4] = rs.getString("SD");
                        data[count][5] = rs.getString("CLASS");
                        count++;


                    }
                    JTable table = new JTable(data, title);
                    JScrollPane scrollPane = new JScrollPane(table);
                    DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
                    cr.setHorizontalAlignment(SwingConstants.CENTER);
                    table.setDefaultRenderer(Object.class, cr);

                    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
                    table.getColumnModel().getColumn(0).setPreferredWidth(300);
                    table.getColumnModel().getColumn(1).setPreferredWidth(200);
                    table.getColumnModel().getColumn(2).setPreferredWidth(200);
                    table.getColumnModel().getColumn(3).setPreferredWidth(200);
                    table.getColumnModel().getColumn(4).setPreferredWidth(200);
                    table.getColumnModel().getColumn(5).setPreferredWidth(200);


                    table.setRowHeight(70);
                    table.setFont(new Font("微软雅黑", Font.BOLD, 17));
                    scrollPane.setBounds(70, 30, 650, 90);
                    frame3.add(scrollPane);

                } catch (ClassNotFoundException cnfe) {
                    cnfe.printStackTrace();
                    JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
                } catch (SQLException sqle) {
                    sqle.printStackTrace();
                    JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
                }

        frame3.setVisible(true);

    }
}